<?php

namespace App\Console\Commands\SiteLocation;

use App\Console\Commands\Poi\BusinessPoi;
use App\Models\Common\MapNationalIndustrialParkModel;
use App\Models\Common\MapNationalStreetModel;
use App\Models\Common\MapNationalUniversitiesModel;
use App\Models\Common\MapTownshipPlanAreaModel;
use App\Models\OpenShop\Common\SiteLocationMallModel;
use App\Service\Common\AreaService;
use Illuminate\Console\Command;

class InitProvinceCityCode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'siteLocation:initProvinceCityCode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化点位各种表省市区Code';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $arr = [
            SiteLocationMallModel::class,
            MapNationalStreetModel::class,
            MapTownshipPlanAreaModel::class,
            MapNationalUniversitiesModel::class,
            MapNationalIndustrialParkModel::class,
        ];

        foreach ($arr as $class) {
            $model = new $class();
            $model::query()
                ->where('provinceCode', 0)
                ->chunkById(
                    100, function ($mall) {
                    foreach ($mall as $v) {
                        $regionInfo['province']['code'] = 0;
                        if ($v['province'] && $v['city'] && $v['district']) {
                            $regionInfo = AreaService::getAdcodeByProvinceCityDistrict($v['province'], $v['city'], $v['district']);
                        }
                        if ($regionInfo['province']['code'] == 0 && $v->lng && $v->lat) {
                            $administration = BusinessPoi::getAdministrationByCode($v->lng, $v->lat);
                            if ($administration['province'] == '北京市' && $administration['city'] != '北京市') {
                                $administration['city'] = '北京市';
                            }
                            if ($administration['province'] == '上海市' && $administration['city'] != '上海市') {
                                $administration['city'] = '上海市';
                            }
                            if ($administration['province'] == '天津市' && $administration['city'] != '天津市') {
                                $administration['city'] = '天津市';
                            }
                            if ($administration['province'] == '重庆市' && $administration['city'] != '重庆市') {
                                $administration['city'] = '重庆市';
                            }
                            $regionInfo     = AreaService::getAdcodeByProvinceCityDistrict($administration['province'], $administration['city'], $administration['district']);
                        }
                        if ($regionInfo['province']['code'] == 0) {
                            continue;
                        }

                        $v->province     = $regionInfo['province']['name'];
                        $v->provinceCode = $regionInfo['province']['code'];
                        $v->city         = $regionInfo['city']['name'];
                        $v->cityCode     = $regionInfo['city']['code'];
                        $v->district     = $regionInfo['district']['name'];
                        $v->districtCode = $regionInfo['district']['code'];
                        $v->save();

                        $this->info($v->id . '-' . json_encode($regionInfo, JSON_UNESCAPED_UNICODE) . '-' . $v->wgNumber);
                    }
                }
                );
        }
    }
}
